
let player=null
let hotKeys=window.hotKeys
// let electronApi=window.electronApi

Vue.createApp({
  components:{
    dropbox:window.vuecom_dropbox,
    slider:window.vuecom_slider
  },
  data(){
    return {
      // 当前播放视频
      currentFile:null,
      // 文件列表
      fileList:[],
      // 文件序号
      fileIndex:0,
      // 是否在播放
      isPlaying:false,
      // 是否静音
      isMuted:false,
      // 音量
      volume:0.1,
      // 视频时长
      duration:0,
      // 当前时间
      currentTime:0,
      // 是否循环
      isLoop:false,
      // 视频旋转角度
      rotateDeg:0,
      // 移动计时器
      moveTimer:0,
      // 右上角是否显示
      trShow:false
    }
  },
  computed:{
    durationText(){
      return formatterSeconds(this.duration)
    },
    currentTimeText(){
      return formatterSeconds(this.currentTime)
    },
    videoPrecent(){
      return this.currentTime/this.duration
    }
  },
  methods:{
    // 视频拖入UI
    dropHandle(file){
      if (electronApi){
        let fs=electronApi.fs
        this.currentFile=file.path
        let filePath=file.path.replace(file.name,'')
        this.fileList=fs.readdirSync(filePath).filter((item)=>{
          return getSuffix(item)==='mp4'
        })
        this.fileList=this.fileList.map((item,index)=>{
          if ((filePath+item)===this.currentFile){
            this.fileIndex=index
          }
          return filePath+item
        })
      }else{
        this.fileIndex=0
        this.currentFile=URL.createObjectURL(file)
        this.fileList=[this.currentFile]
      }

      this.videoLoad(this.currentFile)

    },
    // 视频加载完成后
    videoLoad(src){
      player.src=src
      player.play()
      
    },
    // 进度条变化
    processHandle(val){
      if (!this.currentFile){
        return
      }
      player.currentTime=player.duration*val
    },
    // 开始按钮
    playBtnHandle(){
      if (player.paused){
        player.play()
      }else{
        player.pause()
      }
    },
    // 静音按钮
    mutedHandle(){
      this.isMuted=!this.isMuted
      player.muted=this.isMuted
      setCfg('player_cfg_muted',this.isMuted)

    },
    // 循环按钮
    loopHandle(){
      this.isLoop=!this.isLoop
      setCfg('player_cfg_loop',this.isLoop)

    },
    // 下一个
    nextVideo(){
      if (this.fileIndex+1<this.fileList.length){
        this.fileIndex++
      }else{
        this.fileIndex=0
      }
      this.currentFile=this.fileList[this.fileIndex]
      this.videoLoad(this.currentFile)
    },
    // 上一个
    prevVideo(){
      if (this.fileIndex-1>-1){
        this.fileIndex--
      }else{
        this.fileIndex=this.fileList.length-1
      }
      this.currentFile=this.fileList[this.fileIndex]
      this.videoLoad(this.currentFile)
    },
    // 全屏
    fullscreenHandle(){
      // player.requestFullscreen()
    },
    // 表面移动计时
    mouseMoveHandle(){
      this.trShow=true
      clearTimeout(this.moveTimer)
      this.moveTimer=setTimeout(()=>{
        this.trShow=false
      },3000)
    },
    // 右上角
    trHandle(str){
      electronApi.setWindowState(str)
    },
    // 播放器初始化
    playerInit(){
      this.volume=getCfg('player_cfg_volume')
      this.muted=getCfg('player_cfg_muted')
      this.isLoop=getCfg('player_cfg_loop')

      player.ontimeupdate=()=>{

        this.duration=player.duration
        this.currentTime=player.currentTime
      }

      player.onplay=()=>{
        this.isPlaying=true
      }

      player.onpause=()=>{
        this.isPlaying=false
      }

      player.onended=()=>{
        this.isPlaying=false
        if (this.isLoop){
          player.play()
        }else{
          this.nextVideo()
        }
      }

      player.onvolumechange=()=>{
        this.volume=player.volume
      }

      document.onkeydown=(event)=>{
        let kc=event.keyCode
        console.log(kc)
        switch (kc){
          case hotKeys.play:
            this.playBtnHandle()
            break
          case hotKeys.fastForward:
            player.currentTime+=3
            break
          case hotKeys.fastWind:
            player.currentTime-=3
            break
          case hotKeys.volumeUp:
            player.volume+=0.1
            break
          case hotKeys.volumeDown:
            player.volume-=0.1
            break
          case hotKeys.nextVideo:
            this.nextVideo()
            break
          case hotKeys.prevVideo:
            this.prevVideo()
            break
          case hotKeys.rotateVideo:
            this.rotateDeg+=90
            if (this.rotateDeg===360){
              this.rotateDeg=0
            }
            break
        }

      }

      electronApi.getArgvFile().then((res)=>{
        if (res.indexOf('mp4')!==-1){
          let fileArr=res.split('\\')
          this.dropHandle({
            name:fileArr[fileArr.length-1],
            path:res
          })
        }

      })
    }
  },
  watch:{
    volume(val){
      player.volume=val
      setCfg('player_cfg_volume',val)
    },
    rotateDeg(val){
      if (val===90 || val===270){
        window.resizeTo(540,1012)
      }else{
        window.resizeTo(1280,784)

      }
    }
  },
  mounted(){
    this.$nextTick(()=>{
      player=this.$refs['player']
      this.playerInit()
    })
  }
}).mount('#app')